﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;

/// <summary>
/// 本地玩家输入
/// </summary>
public class LocalController : MonoBehaviour {

    public Transform cameraTrs;
    protected Player localPlayer;
    public void SetLocol(Player value)
    {
        localPlayer = value;
        cameraTrs.SetParent(localPlayer.transform);
        cameraTrs.localPosition = new Vector3(0,6,-6);
        cameraTrs.localEulerAngles = new Vector3(30,0,0);
    }

    //保存上次的操作向量
    protected Vector2 lastDirect = Vector2.zero;

    void FixedUpdate()
    {
        if(localPlayer)
        {
            float dx = Input.GetAxisRaw("Horizontal");
            float dy = Input.GetAxisRaw("Vertical");

            Vector2 direct = new Vector2(dx, dy);

            if (lastDirect != direct)
            {
                lastDirect = direct;

                MoveData data = new MoveData();
                data.username = localPlayer.userData.username;
                data.direct = direct;
                data.position = localPlayer.transform.position;
                data.rotation = localPlayer.transform.eulerAngles;

                string json = JsonUtility.ToJson(data);
                NetworkManager.Instance.SendMessage(Protocol.PlayerMove_CMD, json);
            }

            localPlayer.Move(direct);
        }
    }
}
